package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.GradeUserBindApplicationService;
import com.example.wisdomclassroomapi.application.service.ScheduleLessonApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.common.util.TimeUtil;
import com.example.wisdomclassroomapi.domain.entity.GradeUserBindEntity;
import com.example.wisdomclassroomapi.domain.entity.ScheduleLessonEntity;
import com.example.wisdomclassroomapi.facade.assembler.ScheduleLessonFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.ScheduleLessonRequestDto;
import com.example.wisdomclassroomapi.facade.dto.ScheduleLessonResponseDto;
import com.example.wisdomclassroomapi.infrastructure.mapper.ScheduleLessonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-19
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-19
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/scheduleLesson")
@Slf4j
public class ScheduleLessonController extends BaseController {
    @Resource
    private ScheduleLessonApplicationService scheduleLessonApplicationService;

    @Resource
    private GradeUserBindApplicationService gradeUserBindApplicationService;

    @Resource
    private ScheduleLessonMapper scheduleLessonMapper;

    @Resource
    private ScheduleLessonFacadeAssembler scheduleLessonFacadeAssembler;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageDto<List<ScheduleLessonResponseDto>> getScheduleLessonByPage(@RequestBody @Valid ScheduleLessonRequestDto request) {
        IPage<ScheduleLessonEntity> page = new Page<ScheduleLessonEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
        ScheduleLessonEntity entity = scheduleLessonFacadeAssembler.assembleScheduleLessonEntity(request);
        page = scheduleLessonApplicationService.page(entity, page);
        List<ScheduleLessonResponseDto> assembleData = new ArrayList<>();
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            for (ScheduleLessonEntity item : page.getRecords()) {
                assembleData.add(scheduleLessonFacadeAssembler.assembleScheduleLessonResponseDto(item));
            }
        }
        PageDto<List<ScheduleLessonResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
    }


    /**
     * 查询
     */
    @PostMapping("/list")
    public List<ScheduleLessonResponseDto> getScheduleLessons(@RequestBody @Valid ScheduleLessonRequestDto request) {
        ScheduleLessonEntity entity = scheduleLessonFacadeAssembler.assembleScheduleLessonEntity(request);
        List<ScheduleLessonEntity> list = scheduleLessonApplicationService.list(entity);
        List<ScheduleLessonResponseDto> assembleData = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ScheduleLessonEntity item : list) {
                assembleData.add(scheduleLessonFacadeAssembler.assembleScheduleLessonResponseDto(item));
            }
        }
        return assembleData;
    }

    /**
     * 获取某一实体
     */
    @PostMapping("/getById")
    public ScheduleLessonResponseDto getScheduleLessonDetails(@RequestBody ScheduleLessonEntity entity) {
        entity = scheduleLessonApplicationService.findById(entity.getId());
        ScheduleLessonResponseDto responseDto = null;
        if (entity != null) {
            responseDto = scheduleLessonFacadeAssembler.assembleScheduleLessonResponseDto(entity);
        }
        return responseDto;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public void saveScheduleLesson(@RequestBody ScheduleLessonRequestDto request) {
        ScheduleLessonEntity entity = scheduleLessonFacadeAssembler.assembleScheduleLessonEntity(request);
        scheduleLessonApplicationService.add(entity);
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public void modifyScheduleLesson(@RequestBody ScheduleLessonEntity entity) {
//       ScheduleLessonEntity entity= scheduleLessonFacadeAssembler.assembleScheduleLessonEntity(request);
        scheduleLessonApplicationService.updateById(entity);
    }

    /**
     * 删除数据
     */
    @PostMapping("/delete")
    public void removeScheduleLesson(@RequestBody @Valid String id) {
        scheduleLessonApplicationService.deleteById(id);
    }

    @PostMapping("/next")
    public ScheduleLessonResponseDto getNext() {
        String userId = this.getLoginUserId();
        GradeUserBindEntity entity = new GradeUserBindEntity();
        entity.setUserId(userId);
        List<GradeUserBindEntity> list = gradeUserBindApplicationService.list(entity);

        Set<String> gradeIds = new HashSet<>();
        list.forEach(item -> {
            gradeIds.add(item.getGradeId());
        });

        QueryWrapper<ScheduleLessonEntity> wrapper = new QueryWrapper<ScheduleLessonEntity>()
                .in("grade_id", gradeIds)
                .ge("start_time", TimeUtil.now())
                .orderByAsc("start_time")
                .last("limit 1");
        ScheduleLessonEntity scheduleLessonEntity = scheduleLessonMapper.selectOne(wrapper);

        return scheduleLessonFacadeAssembler.assembleScheduleLessonResponseDto(scheduleLessonEntity);
    }


}